package org.aeon.heroicsagatextedition.game.Backup;

import org.aeon.heroicsagatextedition.role.product.RoleUnit;

import java.util.Deque;
import java.util.LinkedList;
import java.util.stream.Collectors;

/**
 * @projectName: HeroicSagaTextEdition
 * @package: org.aeon.heroicsagatextedition.game
 * @className: GameBackup
 * @author: MissingAeon
 * @date: 2023/12/25 12:04
 * @description: 游戏备份相关
 */
public class GameBackup {
    private static Caretaker caretaker;

    /**
     * 创建并保存游戏
     *
     * @param roles
     */
    public static void createMemento(Deque<RoleUnit> roles, int recordCount) {
        caretaker = new Caretaker();
        Deque<RoleUnit> newRoles = rolesClone(roles);
        caretaker.setMemento(new Memento(newRoles, recordCount));
    }

    /**
     * 读取备份
     *
     * @return
     */
    public static Deque<RoleUnit> getRoles() {
        return caretaker.getMemento().getRoles();
    }

    public static int getRecordCount() {
        return caretaker.getMemento().getRecordCount();
    }

    /**
     * 判断是否存在备份
     *
     * @return
     */
    public static boolean isExistenceMemento() {
        return caretaker != null;
    }

    /**
     * 清理备份
     */
    public static void cleanUp() {
        caretaker = null;
    }

    private static Deque<RoleUnit> rolesClone(Deque<RoleUnit> roles) {
        return roles.stream().map(RoleUnit::clone).collect(Collectors.toCollection(LinkedList::new));
    }
}




